package com.chinacoal.hr.levelimpl.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chinacoal.hr.levelapi.api.CapUserClient;
import com.chinacoal.hr.levelapi.entity.Employee;
import com.chinacoal.hr.levelapi.entity.EmployeeVO;
import com.chinacoal.hr.levelapi.util.Result;
import com.chinacoal.hr.levelimpl.dao.EmployeeDao;
import com.chinacoal.hr.levelimpl.service.EmployeeService;
import com.chinacoal.hr.levelimpl.service.OrganizationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-12-04 18:11
 * @description:
 **/
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private CapUserClient capUserClient;

    @Autowired
    public EmployeeDao employeeDao;

    @Autowired
    public OrganizationService organizationService;

    /**
     * 带分页带条件查询员工列表
     *
     * @param condition :
     * @return : com.baomidou.mybatisplus.core.metadata.IPage<com.chinacoal.hr.levelimpl.entity.Employee>
     * @author : YunTao.Li
     * @date : 2019/12/4 2019/12/4
     */
    @Override
    public IPage<Employee> getEmployeeList(Map<String, Object> condition) throws Throwable {
        IPage<Employee> empList = this.employeeDao.getEmployeeslList(condition);
        return empList;
    }

    /**
     * 不带分页查询人员列表
     *
     * @param condition :
     * @return : java.util.List<com.chinacoal.hr.levelimpl.entity.Employee>
     * @author : YunTao.Li
     * @date : 2020/7/20 2020/7/20
     */
    public List<Employee> getEmployeeListWithOutPage(Map<String, Object> condition) throws Throwable {
        List<Employee> employees = this.employeeDao.getEmployeeslListWithOutPage(condition);
        return employees;
    }

    @Override
    public List<Employee> getEmployeeListFuzzyByName(String empName) throws Throwable {
        if (StringUtils.isNotBlank(empName)) {
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("empName", empName);

            List<Employee> employees = this.employeeDao.getEmployeeslListFuzzy(condition);
            return employees;
        } else {
            return null;
        }
    }

    /**
     * 调用中台服务获得员工详情
     *
     * @param empCode :
     * @return : com.chinacoal.hr.levelimpl.entity.Employee
     * @author : YunTao.Li
     * @date : 2020/1/6 2020/1/6
     */
    public Employee getEmployeeDetail(String empCode) throws Throwable {
        Map condition = new HashMap();
        condition.put("empCode",empCode);
        Employee employee =  this.employeeDao.getEmployeeByCondition(condition);

//        Result<List<EmployeeVO>> empsResult = capUserClient.findEmpByEmpCode(empCode);
//        if (empsResult.getCode() == 10000) {
//            EmployeeVO emp = empsResult.getData().get(0);
//            String deptName = emp.getOrgName();
//            //TODO:orgCode
//            String deptCode = "KF47055";
//            String orgCode = emp.getManagerOrgCode();
//            String orgName = emp.getManagerOrgName();
//
//            employee.setDeptCode(deptCode);
//            employee.setDeptName(deptName);
//            employee.setOrgCode(orgCode);
//            employee.setOrgName(orgName);
//            employee.setEmpCode(emp.getEmpCode());
//            employee.setEmpName(emp.getEmpName());
//        } else {
//            throw new Exception(empsResult.getMsg());
//        }
        return employee;
    }

    /**
     * 通过本地数据库表获得员工详情
     *
     * @param empCode :
     * @return : com.chinacoal.hr.levelimpl.entity.Employee
     * @author : YunTao.Li
     * @date : 2020/1/7 2020/1/7
     */
    public Employee getEmployeeDetailLocal(String empCode) throws Throwable {
        try {
            // step1 : 通过员工编码查询员工详情
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("empCode", empCode);
            Employee employee = this.employeeDao.getEmployeeByCondition(condition);
            if (employee == null) {
                return null;
            }

//            Organization department = null;
//            try {
//                // step2 : 获得dept信息
//                String orgId = employee.getOrgId();
//                department = organizationService.getOrganization(orgId);
//                String deptCode = department.getOrgCode();
//                String deptName = department.getOrgName();
//                employee.setDeptCode(deptCode);
//                employee.setDeptName(deptName);
//            } catch (Exception e1) {
//                System.out.println("获得dept信息失败 empcode:" + employee.getEmpCode() + "orgId:" + employee.getOrgId());
//            }
//            try {
//                // step3 : 获得org信息
//                String parentOrgId = department.getParentOrgId();
//                Organization organization = organizationService.getOrganizationRecursion(parentOrgId);
//                String orgCode = organization.getOrgCode();
//                String orgName = organization.getOrgName();
//                employee.setOrgCode(orgCode);
//                employee.setOrgName(orgName);
//            } catch (Exception e2) {
//                System.out.println("获得dept信息失败 empcode:" + employee.getEmpCode() + "orgId:" + employee.getOrgId());
//            }

            return employee;
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
    }
}
